package 观察者模式;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: wh(1835734390 @ qq.com)
 * @date: 2023/8/9 13:49
 * @description:
 * @version:
 */
public class Subject {
    private List<Observer> observers = new ArrayList<>();
    private double stockPrice;

    public void attach(Observer observer) {
        observers.add(observer);
    }

    public void detach(Observer observer) {
        observers.remove(observer);
    }

    public void setStockPrice(double price) {
        this.stockPrice = price;
        notifyObservers(price);
    }

    private void notifyObservers(double price) {
        for (Observer observer : observers) {
            observer.update(price);
        }
    }
}
